<?php

namespace Gds\VenteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Gds\VenteBundle\Entity\Caisse;
use Gds\VenteBundle\Form\CaisseType;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Ps\PdfBundle\Annotation\Pdf;
class CaisseController extends Controller {

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
            $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code)";
            $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%'));
        } else {
            $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code and c.departement=:departement)";
            $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%', 'departement' => $this->getUser()->getDepartement()->getId()));
        }

        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsVenteBundle:Caisse:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:Caisse:index.html.twig', array('pagination' => $pagination));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function listAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
            $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code)";
            $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%'));
        } else {
            $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code and c.departement=:departement)";
            $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%', 'departement' => $this->getUser()->getDepartement()->getId()));
        }$paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsVenteBundle:Caisse:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:Caisse:list.html.twig', array('pagination' => $pagination));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function newAction() {
        $caisse = new Caisse();
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $societe = $em->getRepository('GdsArticleBundle:Societe')->findAll();
        $caisse->setCode($societe[0]->getPrefixeCaisse() . "-" . sprintf("%04s", $societe[0]->getNumeroCaisse() + 1));
        $caisse->setIsActive(false);
        $caisse->setIsSupprimer(false);
        $form = $this->createForm(new CaisseType(), $caisse);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $caisse = $form->getData();
                $caisse->setCode($societe[0]->getPrefixeCaisse() . "-" . sprintf("%04s", $societe[0]->getNumeroCaisse() + 1));
                $societe[0]->setNumeroCaisse($societe[0]->getNumeroCaisse() + 1);
                $em->persist($societe[0]);
                $em->persist($caisse);
                $em->flush();
                $red = $request->get('red');
                if ($red == 'n') {
                    $caisse = new Caisse();
                    $caisse->setCode($societe[0]->getPrefixeCaisse() . "-" . sprintf("%04s", $societe[0]->getNumeroCaisse() + 1));
                    $caisse->setIsActive(false);
                    $caisse->setIsSupprimer(false);
                    $form = $this->createForm(new CaisseType(), $caisse);
                    return $this->render('GdsVenteBundle:Caisse:new.html.twig', array('form' => $form->createView()));
                } else {
                    $em = $this->getDoctrine()->getEntityManager();
                    $request = $this->getRequest();
                    if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
                        $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code)";
                        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%'));
                    } else {
                        $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code and c.departement=:departement)";
                        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%', 'departement' => $this->getUser()->getDepartement()->getId()));
                    }$paginator = $this->get('knp_paginator');
                    $pagination = $paginator->paginate(
                            $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                    );
                    $pagination->setUsedRoute('gds_caisse_list');
                    if ($request->get('type')) {
                        return $this->render('GdsVenteBundle:Caisse:pageList.html.twig', array('pagination' => $pagination));
                    }
                    return $this->render('GdsVenteBundle:Caisse:list.html.twig', array('pagination' => $pagination));
                }
            }
        }
        return $this->render('GdsVenteBundle:Caisse:new.html.twig', array('form' => $form->createView()));
    }

    /**
     * @Secure(roles="ROLE_SUPER_ADMIN")
     */
    public function deleteAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $caisse = $em->getRepository('GdsVenteBundle:Caisse')->find($id);
        $caisse->setIsSupprimer(true);
        $em->persist($caisse);
        $em->flush();
        $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_caisse_list');
        if ($request->get('type')) {
            return $this->render('GdsVenteBundle:Caisse:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsVenteBundle:Caisse:list.html.twig', array('pagination' => $pagination));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     */
    public function editAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $caisse = $em->getRepository('GdsVenteBundle:Caisse')->find($id);
        $form = $this->createForm(new CaisseType(), $caisse);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $caisse = $form->getData();
                $em->persist($caisse);
                $em->flush();
                $em = $this->getDoctrine()->getEntityManager();
                $request = $this->getRequest();
                if ($this->getUser()->hasRole('ROLE_SUPER_ADMIN')) {
                    $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code)";
                    $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%'));
                } else {
                    $dql = "SELECT c FROM GdsVenteBundle:Caisse c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle and c.code LIKE :code and c.departement=:departement)";
                    $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%', 'code' => '%' . $request->get('code') . '%', 'departement' => $this->getUser()->getDepartement()->getId()));
                }$paginator = $this->get('knp_paginator');
                $pagination = $paginator->paginate(
                        $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                );
                $pagination->setUsedRoute('gds_caisse_list');
                if ($request->get('type')) {
                    return $this->render('GdsVenteBundle:Caisse:pageList.html.twig', array('pagination' => $pagination));
                }
                return $this->render('GdsVenteBundle:Caisse:list.html.twig', array('pagination' => $pagination));
            }
        }
        return $this->render('GdsVenteBundle:Caisse:edit.html.twig', array('id' => $caisse->getId(), 'form' => $form->createView()));
    }
/**
 * @Pdf()
 */
    public function imprimeTicketAction() {
        $format = $this->get('request')->get('_format');
        return $this->render('GdsVenteBundle:Caisse:helloAction.pdf.twig');
    }
    public function pointVenteAction(){
        return $this->render(sprintf('GdsVenteBundle:Caisse:pointVente.html.twig'));
    }

}
